// Widgets/Home/LuxuryCarPackage/LuxuryCarPackage.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { queryAppInforMation } from '../../../api/brand';
import { queryTravelVehicleInfo } from '../../../api/vehicle';
import { addZero , getDateWeek } from '../../../utils/util';
Component({
  options: {
    styleIsolation: 'shared'
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    rentCarStartCurrent: '',
    rentCarStartDate: '', // 开始时间的日期
    rentCarStartWeek: '', // 开始时间的星期
    rentCarStartTime: '10:00', // 开始租车时间
    rentCarDateDiff: '2', // 租还间隔时间
    rentCarStartYear: '',
    rentCarEndCurrent: '',
    rentCarEndDate: '', // 还车时间的日期
    rentCarEndWeek: '', // 还车时间的星期
    rentCarEndTime: '10:00', // 还车时间
    rentCarEndYear: '',
    minDate: new Date().getTime(),
    showStartTimePop: false,
    showEndTimePop: false,
    dataList: [],
    page: 0,
    isReachBottom: false,
    noMore: false,
    loading: false,
    loadingFailed: false,
  },
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      this.updateRentCarDate();
      this.getBannerList();
      this.getData();
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    updateRentCarDate(){
      var nowDate = new Date();
      let rentCarEndCurrent = wx.getStorageSync('rentCarEndCurrent');
      let rentCarEndYear = wx.getStorageSync('rentCarEndYear');
      let rentCarEndDate = wx.getStorageSync('rentCarEndDate');
      let rentCarEndTime = wx.getStorageSync('rentCarEndTime');
      let rentCarDateDiff = wx.getStorageSync('rentCarDateDiff');
      let rentCarStartCurrent = wx.getStorageSync('rentCarStartCurrent');
      let rentCarStartYear = wx.getStorageSync('rentCarStartYear');
      let rentCarStartDate = wx.getStorageSync('rentCarStartDate');
      let rentCarStartTime = wx.getStorageSync('rentCarStartTime');
      let rentCarStartWeek = wx.getStorageSync('rentCarStartWeek');
      let rentCarEndWeek = wx.getStorageSync('rentCarEndWeek')
      if(!rentCarStartCurrent || parseInt(rentCarStartCurrent) < nowDate.getTime()){
        rentCarStartCurrent = nowDate.getTime() + 24 * 3600 * 1000;
        var start = new Date(rentCarStartCurrent);
        rentCarStartYear = start.getFullYear();
        rentCarStartDate = `${addZero(start.getMonth() + 1)}-${addZero(start.getDate())}`;
        rentCarStartTime = `${addZero(start.getHours())}:${addZero(start.getMinutes())}`;
        rentCarStartWeek = `星期${getDateWeek(start)}`
        rentCarEndCurrent = rentCarStartCurrent + 2 * 24 * 3600 * 1000;
        let end = new Date(rentCarEndCurrent);
        rentCarEndYear = end.getFullYear();
        rentCarEndDate = `${addZero(end.getMonth() + 1)}-${addZero(end.getDate())}`;
        rentCarEndTime = `${addZero(end.getHours())}:${addZero(end.getMinutes())}`;
        rentCarEndWeek = `星期${getDateWeek(end)}`
        rentCarDateDiff = 2;
      }
      this.setData({
        rentCarStartCurrent: rentCarStartCurrent,
        rentCarStartDate: rentCarStartDate,
        rentCarStartTime: rentCarStartTime,
        rentCarEndCurrent: rentCarEndCurrent,
        rentCarDateDiff: rentCarDateDiff,
        rentCarEndDate: rentCarEndDate,
        rentCarEndTime: rentCarEndTime,
        rentCarStartWeek: rentCarStartWeek,
        rentCarEndWeek: rentCarEndWeek,
        rentCarStartYear: rentCarStartYear,
        rentCarEndYear: rentCarEndYear,
      })
      wx.setStorageSync('rentCarStartCurrent', rentCarStartCurrent);
      wx.setStorageSync('rentCarStartDate', rentCarStartDate);
      wx.setStorageSync('rentCarStartTime', rentCarStartTime);
      wx.setStorageSync('rentCarEndCurrent', rentCarEndCurrent);
      wx.setStorageSync('rentCarDateDiff', rentCarDateDiff);
      wx.setStorageSync('rentCarEndDate', rentCarEndDate);
      wx.setStorageSync('rentCarEndTime', rentCarEndTime);
      wx.setStorageSync('rentCarStartWeek', rentCarStartWeek);
      wx.setStorageSync('rentCarEndWeek', rentCarEndWeek);
      wx.setStorageSync('rentCarStartYear', rentCarStartYear);
      wx.setStorageSync('rentCarEndYear', rentCarEndYear);
    },
    showStart(){
      this.setData({
        showStartTimePop: true
      })
    },
    startConfirm(e){
      console.log('start',e)
      var date = new Date(e.detail)
      console.log(date,'date')
      var year = date.getFullYear();
      var month = addZero(date.getMonth() + 1);
      var day = addZero(date.getDate());
      var hour = addZero(date.getHours());
      var min = addZero(date.getMinutes())
      var week = `星期${getDateWeek(date)}`;
      this.setData({
        showStartTimePop: false,
        showEndTimePop: true,
        rentCarStartCurrent: e.detail,
        rentCarStartDate: `${month}-${day}`,
        rentCarStartTime: `${hour}:${min}`,
        rentCarStartWeek: week,
        rentCarStartYear: year,
      })
    },
    startCancel(){
      this.setData({
        showStartTimePop: false,
      })
    },
    showEnd(){
      this.setData({
        showEndTimePop: true,
      })
    },
    endConfirm(e){
      console.log('end',e)
      var date = new Date(e.detail)
      console.log(date,'date')
      var year = date.getFullYear();
      var month = addZero(date.getMonth() + 1);
      var day = addZero(date.getDate());
      var hour = addZero(date.getHours());
      var min = addZero(date.getMinutes())
      if(e.detail - this.data.rentCarStartCurrent < 30 * 60 * 1000){
        wx.showToast({
          title: '租车时间和换成时间暂无法选择同一时间段',
          icon: 'none',
        });
        return;
      } 
      var diffTime = e.detail - this.data.rentCarStartCurrent + 60000;
      var diffDay = parseInt(diffTime / 24 / 3600 / 1000);
      var endWeek = `星期${getDateWeek(date)}`;
      this.setData({
        showEndTimePop: false,
        rentCarEndYear: year,
        rentCarEndCurrent: e.detail,
        rentCarEndDate: `${month}-${day}`,
        rentCarEndTime: `${hour}:${min}`,
        rentCarDateDiff: diffDay,
        rentCarEndWeek: endWeek,
      })
      wx.setStorageSync('rentCarEndCurrent', e.detail);
      wx.setStorageSync('rentCarEndYear', this.data.rentCarEndYear);
      wx.setStorageSync('rentCarEndDate', this.data.rentCarEndDate);
      wx.setStorageSync('rentCarEndTime', this.data.rentCarEndTime);
      wx.setStorageSync('rentCarDateDiff', this.data.rentCarDateDiff);
      wx.setStorageSync('rentCarStartCurrent', this.data.rentCarStartCurrent);
      wx.setStorageSync('rentCarStartDate', this.data.rentCarStartDate);
      wx.setStorageSync('rentCarStartTime', this.data.rentCarStartTime);
      wx.setStorageSync('rentCarStartYear', this.data.rentCarStartYear);
    },
    endCancel(){
      this.setData({
        showStartTimePop: true,
        showEndTimePop: false
      })
    },
    onClose(){
      this.setData({
        showStartTimePop: false,
        showEndTimePop: false,
      })
    },
    // 获取轮播图信息
    getBannerList() {
      let _this = this
      queryAppInforMation({infoType: '6'})
      .then(res => {
        if (res.success) {
          _this.setData({
            bannerList: res.data.content
          })
        } else {
          wx.showToast({
            title: res.errMessage,
            icon: 'none'
          })
        }
      })
    },
    // 获取豪车列表
    getData(){
      let _this = this;
      queryTravelVehicleInfo({page: _this.data.page,})
      .then(res => {
        if(res.success){
          var list = _this.data.dataList;
          var arr = [];
          for (let index = 0; index < res.data.content.length; index++) {
            const item = res.data.content[index];
            item.instruction = item.instruction.replace(/,/g,'|')
            item.tagList = item.tag.split(',');
            arr.push(item);
          }
          if(_this.data.page == 0){
            list = arr;
          }else{
            list = list.concat(arr);
          }
          _this.setData({
            dataList: list,
            total: res.data.totalElements,
            page: _this.data.page+1,
          })
          if(_this.data.page * 10 >= res.data.totalElements){
            _this.setData({
              isReachBottom: false,
              noMore: true,
              loading: false,
              loadingFailed: false,
            })
          }else{
            _this.setData({
              isReachBottom: true,
              noMore: false,
              loading: false,
              loadingFailed: false,
            })
          }
        }else{
          wx.showToast({
            title: res.errMessage,
            icon: 'none',
          })
        }
      })
    },
    // 跳转详情
    goDetail(e){
      let item = e.target.dataset.item||e.currentTarget.dataset.item;
      let url = e.target.dataset.name||e.currentTarget.dataset.name;
      wx.navigateTo({
        url: `${url}?item=${JSON.stringify(item)}`,
      })
    },
    // 选择车型
    goSearch(){
      wx.navigateTo({
        url: `/pages/Home/VehicleSearch/VehicleSearch`,
      })
    },
    // 刷新数据
    onRefresh(){
      this.setData({
        page: 0,
        isReachBottom: false,
        noMore: false,
        loading: false,
        loadingFailed: false,
      })
      this.getData();
    },
    // 加载更多
    onLoadMore(){
      if(this.data.isReachBottom){
        this.getData()
      }
    },
  }
})